package com.akjava.lib.android.sound;

import java.util.ArrayList;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class MultiPlayMixer {
ArrayList<Player> players=new ArrayList<Player>();
	public class Player{
		public Player(Context context,int resourceId){
			mediaPlayer=MediaPlayer.create(context, resourceId);
			/*
			mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
				public void onCompletion(MediaPlayer mp) {
					//ここでparepare呼ぶとエラー出ます。
					playing=false;
				}});*/
			
		}
		public boolean reservPlay;
		public boolean playing;	//TODO media のplayingのチェックした方がいい?
		public MediaPlayer mediaPlayer;
	}
	
	public void update(){
		for (int i = 0; i <players.size(); i++) {
			if(players.get(i).reservPlay && !players.get(i).mediaPlayer.isPlaying()){
				//players.get(i).playing=true;
				players.get(i).reservPlay=false;
				players.get(i).mediaPlayer.start();
				Log.i("myapp", "play");
				
			}
		}
	}
	public int countPlayer(){
		return players.size();
	}
	public void reservePlay(int index){
		players.get(index).reservPlay=true;
	}
	
	public void finishPlay(int index){
		players.get(index).playing=false;
	}
	public Player getPlayer(int index){
		return players.get(index);
	}
	
	public void addSound(Context context,int resourceId){
		Player player=new Player(context,resourceId);
		
		players.add(player);
	}
}
